﻿
using MySql.Data.MySqlClient;
using server.Model;
using server.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace  server.DAO
{
    public class UserDao : CommonDao
    {
        public void RegisterUser(PubgSession session, string body, string telephone, string password,
            string name, string icon, string checkCode, string userType)
        {
            // Logger.InfoFormat("新的客户端断开：{0}", session.RemoteEndPoint);
            string sql = "select * from user where telephone = @telephone";
            int result = MySqlExecuteTools.GetCountResult(sql,
                new MySqlParameter[] { new MySqlParameter("@telephone", telephone) });

            DataResult dataResult = new DataResult();
            if (result > 0)
            {
                dataResult.result = 1;
                dataResult.reason = "手机号码已注册，请重试";
                session.Send(GetSendData(dataResult, body));
                return;
            }
           
            sql = "select * from user where name = @name";
            result = MySqlExecuteTools.GetCountResult(sql,
                new MySqlParameter[] { new MySqlParameter("@name", name) });

            if(result>0)
            {
                dataResult.result = 1;
                dataResult.reason = "名称重复，请重试";
                session.Send(GetSendData(dataResult, body));
                return;
            }

            int type = Convert.ToInt32(userType);
            dataResult.result = 0;

            long newUserId = 0;
            //玩家或者管理员
            if (type == 0 || type == 1)
            {
                sql = "insert into user(password,name ,telephone,image,type) " +
                "values('" + password + "','" + name + "','" + telephone + "','" + icon + "','" + type + "')";
                newUserId = MySqlExecuteTools.GetAddID(sql);
            }
            else
            {
                sql = "insert into user(password,name ,telephone,image,type,playerType) " +
               "values('" + password + "','" + name + "','" + telephone + "','" + icon + "','0','1')";
                newUserId = MySqlExecuteTools.GetAddID(sql);
            }
            

            //玩家,增加生命信息
            if (type == 0 || type == 2)
            {
                
                sql = "insert into life(userId) " +
                "values('" + newUserId + "')";
                MySqlExecuteTools.AddOrUpdate(sql);
            }
            //管理员，增加一条grounp数据
            if(type == 1)
            {
                sql = "insert into grounp(name,userId) " +
                "values('" + name + "','" + newUserId + "')";
                long newGrounpId = MySqlExecuteTools.GetAddID(sql);
                // Console.WriteLine("newGrounpId="+ newGrounpId.ToString());
                //插入grounp_life

                sql = "insert into grounp_life(grounp_id) " +
                    "values('" + newGrounpId + "')";
                MySqlExecuteTools.AddOrUpdate(sql);
            }
           

            session.Send(GetSendData(dataResult, body));
        }

        public Life GetLifeById(int userId)
        {
            string sql = "select * from life where userId = @userId";
            Life life = MySqlExecuteTools.GetObjectResult<Life>(sql,
                new MySqlParameter[] { new MySqlParameter("@userId", userId) }).FirstOrDefault<Life>();

            return life;
        }

     

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public string ModifyUser(string userId, string userName, string oldPassword, string newPassword,string userType)
        {
           
            UserItem userItem = MySqlExtendTools.GetEntityObjectByPrimaryKey<UserItem>(userId, "user");
            
            //校验密码是否符合
            if(!userItem.password.Equals(oldPassword))
            {
                return "-1";
            }

            UserItem _userItem = MySqlExtendTools.GetEntityObjectByPrimaryKey<UserItem>(userId, "user");

            //管理员
            if(userItem.type == 1)
            {
                string sql = "update user set name = '" + userName +
              "', password = '" + newPassword + "' where id = " + userId;
                MySqlExecuteTools.GetCountResult(sql);
                Console.WriteLine(sql);
            }
            else
            {
                int _usertype = 0;
                int playerType = 0;
                if(userType.Equals("2"))
                {
                    playerType = 1;
                }

                string sql = "update user set name = '" + userName +
              "', password = '" + newPassword + "', type = '" + _usertype + "', playerType = '" + playerType + "' where id = " + userId;
                MySqlExecuteTools.GetCountResult(sql);
                Console.WriteLine(sql);
            }

            return "0";
           
        }

        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <param name="telephone"></param>
        /// <param name="userName"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public string ForgetPassword(string telephone, string newPassword)
        {



            telephone = telephone.Trim();
           // userName = userName.Trim();
            newPassword = newPassword.Trim();
            string sql  = "select * from user where telephone ="+ telephone;
            int count = MySqlExecuteTools.GetCountResult(sql);
            if(count==0)
            {
                return "-1";
            }

            //sql = "select * from user where telephone =" + telephone + " and name = '"+ userName +"'";
            sql = "select * from user where telephone =" + telephone;

            count = MySqlExecuteTools.GetCountResult(sql);
            if (count == 0)
            {
                return "-2";
            }

            //更新
             sql = "update user set password = '" + newPassword + "' where telephone = " + telephone;
            MySqlExecuteTools.GetCountResult(sql);

            return "0";
        }

    }

}
